#!/bin/bash
export ii=$II
export i=$I
#set -x
#if [[ "$1" != "-x" ]] ; then
#  grepf.ksh -x $* > why 2>&1
#else
#  shift 1
#fi
eargs1=
eargs2=
oargs1=
oargs2=
if [[ "$1" = "-xp" ]] ; then
	eargs1="-e"
	eargs2="$2"
	shift 2
elif [[ "$1" = "-op" ]] ; then
	oargs1="-e"
	oargs2="$2"
	shift 2
fi
adir=$1
file_pattern="$2"
shift 2
icount=0
jcount=0
#set -x
while (( $# > 0 )) ; do
  cparm="${1%%-*}"
  if [[ -z "$cparm" ]] ; then
	  dparms[$icount]="$1"
	  (( icount = icount + 1 ))
  else
	  sparms[$jcount]="$1"
	  (( jcount = jcount + 1 ))
  fi
  shift 1
done
#set +x
fls="$(find $adir -follow ! -path '*/target/*' ! -path '*/build/*' ! -path '*/.svn/*' -name "$file_pattern")"
grep ${dparms[*]} ${sparms[*]} /etc/passwd $fls | cat -v > $ii/whyg
if [[ -z "$oargs1" ]] ; then
	firstdirs="-e /presrc -e /testpresrc -e /compatpresrc"
else
	firstdirs="$oargs1 $oargs2"
fi
grep $firstdirs $ii/whyg | \
grep -v -e /src -e /archive-events/ -e /archive/ $eargs1 $eargs2
grep -v -e /presrc -e /testpresrc -e /compatpresrc -e /archive-events \
	 -e /archive/ $ii/whyg
grep -e /archive-events $ii/whyg
grep -e /archive/ $ii/whyg
#find $adir/src/java ! -follow -path '*/target/*' -name "$file_pattern" -exec grep ${parms[*]} /etc/passwd {} \; | cat -v
#find $adir \( ! -path '*/target/*' -a -path '*/verityconnector/*' \) -name "$file_pattern" -exec grep ${parms[*]} /etc/passwd {} \; | cat -v
#find $adir \( ! -path '*/target/*' -a -path '*/junitdoclet/*' \) -name "$file_pattern" -exec grep ${parms[*]} /etc/passwd {} \; | cat -v
